﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

using XS.Tunnel.Cad.SteelBracing.Infrastructure.Extensions;

namespace XS.Tunnel.Cad.SteelBracing.Component.SteelShotcrete.GrilleComponent
{
    /// <summary>
    /// 钢筋支撑
    /// </summary>
    public abstract class Grille
    {
        /// <summary>
        /// U型钢筋
        /// </summary>
        public URebar URebar { get; set; }

        public abstract Polyline Create(Line line);

        public abstract double Length { get; }

        public virtual double Height { get; set; }
        /// <summary>
        /// 箍筋
        /// </summary>
        public Stirrup Stirrup { get; set; }

    }
    /// <summary>
    /// 矩形格栅
    /// </summary>
    public class RectangleGrille: Grille
    {
        /// <summary>
        /// 水平底部长度 (斜向筋两端水平长度)
        /// </summary>
        public double HorizontalBottomLength { get; set; }
        /// <summary>
        /// 斜向长度  (斜向钢筋斜向水平长度)
        /// </summary>
        public double SidelingHorizontalLength { get; set; }
        /// <summary>
        /// 水平顶部长度(斜向筋中部水平长度)
        /// </summary>
        public double HorizontalTopLength { get; set; }

        public override double Length
        { 
            get
            {
                return 2*HorizontalBottomLength + HorizontalTopLength+2*SidelingHorizontalLength;
            }
        }


        public override Polyline Create(Line line)
        {
            var topL = HorizontalTopLength / 2;
            var vtL = line.Delta.GetNormal();
            var vt = vtL.GetPerpendicularVector().GetNormal();

            var point = line.EndPoint.Add(vt* -Height / 2);
            var p1 = point.Add(vtL * topL);
            var p2l = new Line(p1, p1.Add(vtL * SidelingHorizontalLength));
            var p2v = new Line(p1, p1.Add(vt * Height));
            var p2 = p1.Add(p2l.Delta + p2v.Delta);
            var p3 = p2.Add(vtL * HorizontalBottomLength);

            var p4= point.Add(vtL * -topL);

            var p5l = new Line(p4, p4.Add(vtL * -SidelingHorizontalLength));
            var p5v = new Line(p4, p4.Add(vt * Height));
            var p5 = p4.Add(p5l.Delta + p5v.Delta);
            var p6 = p5.Add(vtL * -HorizontalBottomLength);


            var polys = new Polyline();
            polys.AddVertexAt(0, p3.To2d(), 0, 0, 0);
            polys.AddVertexAt(1, p2.To2d(), 0, 0, 0);
            polys.AddVertexAt(2, p1.To2d(), 0, 0, 0);
            polys.AddVertexAt(3, point.To2d(), 0, 0, 0);
            polys.AddVertexAt(4, p4.To2d(), 0, 0, 0);
            polys.AddVertexAt(5, p5.To2d(), 0, 0, 0);
            polys.AddVertexAt(6, p6.To2d(), 0, 0, 0);

            return polys;

        }


    }
    /// <summary>
    /// 三角格栅
    /// </summary>
    public class TriangleGrille: RectangleGrille
    {
        

    }
    /// <summary>
    /// 八字节格栅
    /// </summary>
    public class FigureOfEightGrille : Grille
    {
      
        public override double Length => Width;

        /// <summary>
        /// 八字节宽度A
        /// </summary>
        public double Width { get; set; }

        /// <summary>
        /// 间距
        /// </summary>
        public double Interval { get; set; }

        /// <summary>
        /// 八字节高度D
        /// </summary>
        public override double Height { get; set; }

        public override Polyline Create(Line line)
        {
            //var vtL = line.Delta.GetNormal();
            //var vt = vtL.GetPerpendicularVector().GetNormal();
            //var p0 = line.StartPoint;
            //var p1=p0.Add(vtL*(-Width/2)).Add(vt*(Height/2));
            //var p2 = p1.Add(vt * -Height);
            //var p3 = line.StartPoint;
            //var p4=p0.Add(vtL*(Width/2)).Add(vt*(Height/2));
            //var p5 = p4.Add(vt * -Height);



            var vtL = line.Delta.GetNormal();
            var vt = vtL.GetPerpendicularVector().GetNormal();
            var p0 = line.EndPoint;
            var p1 = p0.Add(vt*(Height/2));
            var p2= line.StartPoint.Add(vt*-(Height/2));
            var p3 = p2.Add(vt*Height);
            var p4= line.EndPoint.Add(vt*-(Height/2));
            var p5 = line.EndPoint;

            var polys = new Polyline();
            polys.AddVertexAt(0, p0.To2d(), 0, 0, 0);
            polys.AddVertexAt(1, p1.To2d(), 0, 0, 0);
            polys.AddVertexAt(2, p2.To2d(), 0, 0, 0);
            polys.AddVertexAt(3, p3.To2d(), 0, 0, 0);
            polys.AddVertexAt(4, p4.To2d(), 0, 0, 0);
            polys.AddVertexAt(5, p5.To2d(), 0, 0, 0);
            //polys.AddVertexAt(6, p0.To2d(), 0, 0, 0);
            return polys;

        }
    }


    /// <summary>
    /// u型钢筋
    /// </summary>
    public class URebar {

        public double Width { get; set; }

        public double Height { get; set; }


        /// <summary>
        /// 顺时针创建
        /// </summary>
        /// <returns></returns>
        public List<Curve> ClockwiseCreate(Line l)
        {
            var l1 = l.VerticalLine(l.EndPoint, Width);
            var l2 = new Line(l.StartPoint,l.StartPoint.Add(l.Delta.GetNormal()* -Height/2));
            var l3 = l2.VerticalLine(l2.EndPoint, -Width);
            return new List<Curve> { l1,l, l2, l3 };
        }
        /// <summary>
        /// 逆时针创建呢
        /// </summary>
        /// <returns></returns>
        public List<Curve> AnticlockwiseCreate(Line l) {
            var l1 = l.VerticalLine(l.EndPoint, -Width);
            var l2 = new Line(l.StartPoint, l.StartPoint.Add(l.Delta.GetNormal() * -Height / 2));
            var l3 = l2.VerticalLine(l2.EndPoint, Width);
            return new List<Curve> { l1, l, l2, l3 };
        }



    }


    /// <summary>
    /// 箍筋
    /// </summary>
    public class Stirrup {

        public double Height { get; set; }
        public double Interval { get; set; }


    }

}
